﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using SevenShine;
using System;

public class PlayerLevel2 : MonoBehaviour {

    public static PlayerLevel2 Instance;
    private Animator player;
    private AudioSource audios;

    public Transform Target2;
    public Transform Target3;
    public Transform Target4;
    public AudioClip RunClip;
    public AudioClip PassClip;
    public AudioClip Dialog;

    // Use this for initialization
    void Start ()
    {
        Instance = this;
        audios = Camera.main.GetComponent<AudioSource>();
    }

    void ToTarget2()
    {
        PlayerState.Instance.SetState(PlayerState.State.RUN, Target2, 1);

        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        StartCoroutine("ArriveAndTalk");
    }

    public void ToDestination()
    {
        //分数
        GameObject.Find("UI_Game").SendMessage("AddNum", 2);
        //关卡解锁
        if (!LevelSingleton.GetInstance().levelData.ContainsKey(2))
        {
            LevelSingleton.GetInstance().levelData.Add(2, 1);
        }
        

        EraseFade.Instance.audios.clip = EraseFade.Instance.Pass;
        EraseFade.Instance.audios.Play();
        EraseFade.Instance.audios.loop = false;

        PlayerPrefs.SetInt("2", 1);
        StartCoroutine("ToLast");
    }

    IEnumerator ToLast()
    {
        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        PlayerState.Instance.SetState(PlayerState.State.RUN, Target3, 1);
        yield return new WaitForSeconds(1);
        PlayerState.Instance.SetState(PlayerState.State.RUN, Target4, 3);
        yield return new WaitForSeconds(3);
        //audios.Stop();

        audios.clip = PassClip;
        audios.Play();
        audios.loop = false;

        yield return new WaitForSeconds(3);
        //黑屏结束
        IntroduceControl.Instance.Level2LastFade();
        yield return new WaitForSeconds(3);
        //旁白
        IntroduceControl.Instance.Level2LastIntroduce();
    }

    IEnumerator ArriveAndTalk()
    {
        yield return new WaitForSeconds(1);
        audios.Stop();
        GameObject.Find("Role_XiaoQi").GetComponent<Animator>().SetTrigger("Idle1");

        PlayerState.Instance.SetState(PlayerState.State.IDLE, Target2, 1);
        Instantiate(Resources.Load("Prefabs/UI/DialogImage"), GameObject.Find("Canvas").transform);

        Image introduceImage = GameObject.Find("DialogImage(Clone)").GetComponent<Image>();
        Text introduceText = GameObject.Find("Canvas/DialogImage(Clone)/Text").GetComponent<Text>();

        float length = Dialog.length;
        audios.clip = Dialog;
        audios.Play();

        introduceText.text = "";
        introduceText.DOText(LanguageMgr.GetInstance().ShowText("Level2_Dialog"), Dialog.length).SetEase(Ease.Linear).OnComplete(() =>
        {
            audios.Stop();
            EraseFade.Instance.canTouch = true;
            introduceText.DOFade(0, 0.5f);
            introduceImage.DOFade(0, 0.5f);        
        });
    }

    
}
